<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增领用归还记录')"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-transactionRecord-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">资产编号：</label>
            <div class="col-sm-8">
                <input name="assetNumber" class="form-control" type="text" onblur="findAssetName()" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">资产编号名称：</label>
            <div class="col-sm-8">
                <input name="assetName" class="form-control" readonly type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">交易类型：</label>
            <div class="col-sm-8">
                <select id="leixing" name="transactionType" class="form-control m-b"
                        th:with="type=${@dict.getType('asset_tansaction_record')}" required onchange="changeInput()">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
            <input type="checkbox" name="showDiv" value="div1" onchange="changeInput()">补单<br>
        </div>
        <div class="form-group" id="div1" style="display:none;">
            <label class="col-sm-3 control-label">交易日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="transactionDate" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">使用人工号：</label>
            <div class="col-sm-8">
                <input name="borrower" class="form-control " type="text" onblur="findName('borrower','borrowerName')">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">使用人姓名：</label>
            <div class="col-sm-8">
                <input name="borrowerName" class="form-control" readonly type="text">
            </div>
        </div>
        <div class="form-group" hidden="hidden">
            <label class="col-sm-3 control-label">使用人部门：</label>
            <div class="col-sm-8">
                <input name="department" class="form-control" readonly type="text">
            </div>
        </div>
        <div class="form-group" id="div2" style="display:none;">
            <label class="col-sm-3 control-label">归还日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="returnDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group" id="div3" style="display:none;">
            <label class="col-sm-3 control-label">归还人工号：</label>
            <div class="col-sm-8">
                <input name="returner" class="form-control" type="text" onblur="findName('returner','returnerName')">
            </div>
        </div>
        <div class="form-group" id="div4" style="display:none;">
            <label class="col-sm-3 control-label">归还人姓名：</label>
            <div class="col-sm-8">
                <input name="returnerName" class="form-control" readonly type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control"></textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<script th:inline="javascript">
    var prefix = ctx + "asset/transactionRecord"
    $("#form-transactionRecord-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-transactionRecord-add').serialize());
        }
    }

    function changeInput() {
        //获取选中项的值
        var value = $("#leixing option:selected").attr("value");
        //补单按钮是否被选中
        var l = document.getElementsByName("showDiv")[0].checked;
        div1.style.display = 'none';
        div2.style.display = 'none';
        div3.style.display = 'none';
        div4.style.display = 'none';
        if (value == 5) {
            div1.style.display = 'block';//交易时间
            div2.style.display = 'block';//归还时间
            div3.style.display = 'block';//归还人
            div4.style.display = 'block';//归还人姓名
            return;
        }
        if (l) {
            div1.style.display = 'block';//交易时间
        }
    };

    function findAssetName() {
        // 获取工号输入框的值
        var assetNumber = $("input[name='assetNumber']").val();
        // 发送GET请求到后端获取参数
        $.get("/asset/transactionRecord/getAssetName", {assetNumber: assetNumber}, function (response) {
            if (response.code == 0) {
                // 将后端返回的参数设置给使用人输入框
                $("input[name='assetName']").val(response.data);
            } else {
                $("input[name='assetName']").val(response.msg);
            }

        });
    }

    function findName(userId,userName) {
        // 获取工号输入框的值
        var id = document.getElementsByName(userId)[0].value;
        // 发送GET请求到后端获取参数
        $.get("http://172.20.203.11/getoanamedept", {nameid: id}, function (response) {
            var parse = JSON.parse(response);
            if (userName == "borrowerName" && parse.code != "1") {
                $("input[name=borrowerName]").val("");
            }
            if (userName == "returnerName" && parse.code != "1") {
                $("input[name=returnerName]").val("");
            }

            if (parse.code == "1") {
                // 将后端返回的参数设置给使用人输入框
                if (userName=="borrowerName") {
                    $("input[name=borrowerName]").val(parse.name);
                }
                if (userName=="returnerName") {
                    $("input[name=returnerName]").val(parse.name);
                }
                $("input[name=department]").val(parse.parent);
            } else {
                $("input[name=username]").val("没有该名员工");
            }
        });
    }

    $("input[name='transactionDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='returnDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

</script>
</body>
</html>
